﻿using Newtonsoft.Json;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Model.PhysicalExamination;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhysicalExamination5.Service.DataMaintenance
{
    public class SZiDianWeiHu : IZiDianWeiHu
    {
        private readonly ISqlSugarClient db;
        public SZiDianWeiHu(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 查询字典
        /// </summary>
        /// <returns></returns>
        public string GetSelZiDian()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 声明变量

                List<object> listZiDian = new List<object>();

                #endregion

                #region 系统相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "系统相关",
                    children = new List<object>
                    {
                        new {Name = "所属报告类别"},
                        new {Name = "查体类别"},
                        new {Name = "采样类别"},
                        new {Name = "试管类型"},
                        new {Name = "检验性质"},
                        new {Name = "套餐类别"}
                    }
                });

                #endregion

                #region 人员相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "人员相关",
                    children = new List<object>
                    {
                        new {Name = "人员身份"},
                        new {Name = "人员类别"},
                        new {Name = "民族"},
                        new {Name = "照射源"},
                        new {Name = "职业分类及其代号"},
                        new {Name = "工种"}
                    }
                });

                #endregion

                #region 疾病相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "疾病相关",
                    children = new List<object>
                    {
                        new {Name = "排除疾病文字"}
                    }
                });

                #endregion

                #region 质控相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "质控相关",
                    children = new List<object>
                    {
                        new {Name = "质控文字说明"}
                    }
                });

                #endregion

                #region 费用相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "费用相关",
                    children = new List<object>
                    {
                        new {Name = "个人费用结算方式"},
                        new {Name = "单位费用结算方式"}
                    }
                });

                #endregion

                #region 总检结论相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "总检结论相关",
                    children = new List<object>
                    {
                        new {Name = "总检正常结论"},
                        new {Name = "总检审核拒绝原因"}
                    }
                });

                #endregion

                #region 职业病相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "职业病相关",
                    children = new List<object>
                    {
                        new {Name = "职检结论描述"},
                        new {Name = "处理意见"},
                        new {Name = "汇总报告五大类附表排除文字"},
                        new {Name = "职业汇总报告审核拒绝原因"}
                    }
                });

                #endregion

                #region 套餐相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "套餐相关",
                    children = new List<object>
                    {
                        new {Name = "微信套擦标签"}
                    }
                });

                #endregion

                #region 历年对比相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "历年对比相关",
                    children = new List<object>
                    {
                        new {Name = "历年对比参与项目"}
                    }
                });

                #endregion

                #region 附加费用相关

                listZiDian.Add(new
                {
                    Type = 1,
                    Name = "附加费用相关",
                    children = new List<object>
                    {
                        new {Name = "费用类别"},
                        new {Name = "费用性质"}
                    }
                });

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = listZiDian
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 查询字典明细
        /// </summary>
        /// <param name="strYiJiName"></param>
        /// <param name="strErJiName"></param>
        /// <returns></returns>
        public string GetSqlZiDianMingXi(string strYiJiName, string strErJiName)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                List<jcsj_ZiDian> listjcsj_ZiDian = new SqlSugarRepository<jcsj_ZiDian>(db)
                    .getByWhere(t => t.YiJiZiDianMingCheng == strYiJiName && t.ErJiZiDianMingCheng == strErJiName);


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = listjcsj_ZiDian
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 字典明细新增
        /// </summary>
        /// <param name="ziDianMingXiAdd"></param>
        /// <param name="strYuanQuQianZhui"></param>
        /// <returns></returns>
        public string PostZiDianMingXiAdd(ZiDianMingXiAddClass ziDianMingXiAdd,string strYuanQuQianZhui)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 声明变量

                List<jcsj_ZiDian> listjcsj_ZiDian = new List<jcsj_ZiDian>();
                int itid = 0;
                string strSql = "";

                #endregion

                #region 验证数据

                listjcsj_ZiDian = new SqlSugarRepository<jcsj_ZiDian>(db).getByWhere(t => t.YiJiZiDianMingCheng == ziDianMingXiAdd.strYiJiName
                && t.ErJiZiDianMingCheng == ziDianMingXiAdd.strErJiName);
                if (listjcsj_ZiDian.Where(t => t.ZiDianZhiNeiRong == ziDianMingXiAdd.strZiDianZhi).Any())
                {
                    nret = 1;
                    strFanHuiZhi = $@"当前字典下已经存在字典值为【{ziDianMingXiAdd.strZiDianZhi}】！";
                    goto PROC_EXIT;
                }
                if (listjcsj_ZiDian.Where(t => t.ShiFouMoRen == "是").Any() && ziDianMingXiAdd.strShiFouMoRen == "是")
                {
                    nret = 1;
                    strFanHuiZhi = "当前字典下已经存在字典值默认为是的数据！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 保存数据

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    jcsj_ZiDian jcsj_ZiDian = new jcsj_ZiDian();
                    jcsj_ZiDian.YiJiZiDianMingCheng = ziDianMingXiAdd.strYiJiName;
                    jcsj_ZiDian.ErJiZiDianMingCheng = ziDianMingXiAdd.strErJiName;
                    jcsj_ZiDian.ZiDianZhiNeiRong = ziDianMingXiAdd.strZiDianZhi;
                    jcsj_ZiDian.PYM = ziDianMingXiAdd.strPym;
                    jcsj_ZiDian.BeiZhu1 = ziDianMingXiAdd.strBeiZhu1;
                    jcsj_ZiDian.BeiZhu2 = ziDianMingXiAdd.strBeiZhu2;
                    jcsj_ZiDian.ShiFouMoRen = ziDianMingXiAdd.strShiFouMoRen;
                    jcsj_ZiDian.FenLei = ziDianMingXiAdd.strFenLei;
                    itid = new SqlSugarRepository<jcsj_ZiDian>(db).AddReturnIdentity(jcsj_ZiDian);
                    jcsj_ZiDian.tid = itid;
                    jcsj_ZiDian.ZiDianID = strYuanQuQianZhui + itid;
                    if (!new SqlSugarRepository<jcsj_ZiDian>(db).Update(t => new jcsj_ZiDian 
                    { 
                        ZiDianID = jcsj_ZiDian.ZiDianID
                    },t => t.tid == jcsj_ZiDian.tid))
                    {
                        nret = 1000;
                        strFanHuiZhi = "添加失败！";
                        return;
                    }
                    listjcsj_ZiDian.Add(jcsj_ZiDian);
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = listjcsj_ZiDian
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 字典明细修改
        /// </summary>
        /// <param name="ziDianMingXiUpdate"></param>
        /// <returns></returns>
        public string PutZiDianMingXiUpdate(ZiDianMingXiUpdateClass ziDianMingXiUpdate)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                List<jcsj_ZiDian> listjcsj_ZiDian = new SqlSugarRepository<jcsj_ZiDian>(db).getByWhere(t => t.ZiDianID == ziDianMingXiUpdate.strZiDianId);
                listjcsj_ZiDian = new SqlSugarRepository<jcsj_ZiDian>(db).getByWhere(t => t.YiJiZiDianMingCheng == listjcsj_ZiDian[0].YiJiZiDianMingCheng
                && t.ErJiZiDianMingCheng == listjcsj_ZiDian[0].ErJiZiDianMingCheng);
                if (listjcsj_ZiDian.Where(t => t.ZiDianZhiNeiRong == ziDianMingXiUpdate.strZiDianZhi && t.ZiDianID != ziDianMingXiUpdate.strZiDianId).Any())
                {
                    nret = 1;
                    strFanHuiZhi = $@"当前字典下已经存在字典值为【{ziDianMingXiUpdate.strZiDianZhi}】！";
                    goto PROC_EXIT;
                }
                if (listjcsj_ZiDian.Where(t => t.ShiFouMoRen == "是").Any() && ziDianMingXiUpdate.strShiFouMoRen == "是")
                {
                    nret = 1;
                    strFanHuiZhi = "当前字典下已经存在字典值默认为是的数据！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 修改数据

                if (new SqlSugarRepository<jcsj_ZiDian>(db).Update(t => new jcsj_ZiDian 
                { 
                    ZiDianZhiNeiRong = ziDianMingXiUpdate.strZiDianZhi,
                    PYM = ziDianMingXiUpdate.strPym,
                    BeiZhu1 = ziDianMingXiUpdate.strBeiZhu1,
                    BeiZhu2 = ziDianMingXiUpdate.strBeiZhu2,
                    ShiFouMoRen = ziDianMingXiUpdate.strShiFouMoRen,
                    FenLei = ziDianMingXiUpdate.strFenLei
                }, t => t.ZiDianID == ziDianMingXiUpdate.strZiDianId) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "修改失败！";
                    goto PROC_EXIT;
                }

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 字典明细删除
        /// </summary>
        /// <param name="ziDianMingXiDelete"></param>
        /// <returns></returns>
        public string DeleteZiDianMingXiDelete(ZiDianMingXiDeleteClass ziDianMingXiDelete)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                if (new SqlSugarRepository<jcsj_ZiDian>(db).DeleteWhere(t => t.ZiDianID == ziDianMingXiDelete.strZiDianId) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "删除失败！";
                    goto PROC_EXIT;
                }

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
    }
}
